package com.example.network_lib;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.example.base.BaseLibApplication;
import com.example.network_lib.util.ToastUtil;
import com.github.ybq.android.spinkit.dialog.MyLoadingDialog;
import com.github.ybq.android.spinkit.dialog.MySpinKitDialog;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import io.reactivex.disposables.Disposable;

/**
 * @author Administrator
 * @createTime 2019/9/26 10:47
 */
public abstract class MyObserver<T> extends BaseObserver<T> {
    private boolean isShowDialog;
    private ProgressDialog mProgressDialog;
    private Disposable d;
    private int useProgressStyle = 1;
    private MySpinKitDialog myDialog;
    private MyLoadingDialog myLoadingDialog;
    protected String msg = "加载中...";


    public MyObserver(RxAppCompatActivity context) {
        this(context, true);
    }

    public MyObserver(RxAppCompatActivity context, boolean showDialog) {
        isShowDialog = showDialog;
        this.mContext = context;
    }

    public MyObserver(RxAppCompatActivity context, boolean showDialog, String msg) {
        isShowDialog = showDialog;
        this.msg = msg;
        this.mContext = context;
    }


    @Override
    public void onSubscribe(Disposable d) {
        super.onSubscribe(d);
        this.d = d;
        if (!isConnected(BaseLibApplication.getAppContext())) {
            ToastUtil.show("未连接网络");
            if (d.isDisposed()) {
                d.dispose();
            }
        } else {
            switch (useProgressStyle) {
                case 0:
                    if (mProgressDialog == null && isShowDialog) {
                        mProgressDialog = new ProgressDialog(mContext);
                        mProgressDialog.setMessage(msg);
                        mProgressDialog.setCancelable(false);
                        mProgressDialog.setCanceledOnTouchOutside(false);
                        mProgressDialog.show();
                    }
                    break;
                case 1:
                    myDialog = new MySpinKitDialog();
                    myDialog.setCancelable(false);
                    myDialog.show(mContext.getSupportFragmentManager(), "loadingView2");
                    break;
                case 2:
                    myLoadingDialog = new MyLoadingDialog();
                    myLoadingDialog.setCancelable(false);
                    myLoadingDialog.show(mContext.getSupportFragmentManager(), "loadingView2");
                    break;
                default:
                    break;
            }
        }
    }

    private boolean isConnected(Context mContext) {
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo into = cm.getActiveNetworkInfo();
        if (into == null) {
            return false;
        }
        boolean available = into.isAvailable();
        return available;
    }


    @Override
    public void onError(Throwable e) {
        super.onError(e);
        if (d.isDisposed()) {
            d.dispose();
        }
        hideDialog();
    }

    @Override
    public void onComplete() {
        super.onComplete();
        if (d.isDisposed()) {
            d.dispose();
        }
        hideDialog();
    }


    private void hideDialog() {
        switch (useProgressStyle) {
            case 0:
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                }
                mProgressDialog = null;
                break;
            case 1:
                if (myDialog != null && myDialog.getDialog() != null && myDialog.getDialog().isShowing()) {
                    myDialog.dismiss();
                }
                myDialog = null;
                break;
            case 2:
                if (myLoadingDialog != null && myLoadingDialog.getDialog() != null && myLoadingDialog.getDialog().isShowing()) {
                    myLoadingDialog.stop();
                    myLoadingDialog.dismiss();
                }
                myLoadingDialog = null;
                break;
            default:
                break;
        }
    }
}
